﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFFC.Net.Interface;
using EFFC.Net.Common;

namespace EFFC.Net.WebBuilder.WebPage
{
    /// <summary>
    /// WebPageBase根据PageMapping生成的Module
    /// </summary>
    public class PageModule : IModule
    {
        Dictionary<string, object> _v = new Dictionary<string, object>();
        public void SetValue(string columnName, object value)
        {
            if (_v.ContainsKey(columnName))
            {
                _v[columnName] = value;
            }
            else
            {
                _v.Add(columnName, value);
            }
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public T GetValue<T>(string columnName)
        {
            object o = GetValue(columnName);
            if (o != null)
            {
                if (typeof(T) == typeof(bool))
                {
                    if (o.GetType() != typeof(bool))
                    {
                        string s = ComFunc.nvl(o);
                        bool brtn = false;
                        if (s == "1")
                        {
                            brtn = true;
                        }
                        else
                        {
                            brtn = false;
                        }

                        IConvertible convertibleObj = brtn as IConvertible;
                        return (T)(object)convertibleObj.ToBoolean(null);
                    }
                    else
                    {
                        return (T)_v[columnName];
                    }

                }
                else if (o.GetType() == typeof(Int64))
                {
                    Int64 irtn = (Int64)o;
                    IConvertible convertibleObj = irtn as IConvertible;
                    if (typeof(T) == typeof(string))
                    {
                        return (T)(object)convertibleObj.ToString();
                    }
                    else
                    {
                        return (T)_v[columnName]; ;
                    }
                }
                else if (o.GetType() == typeof(decimal))
                {
                    decimal irtn = (decimal)o;
                    IConvertible convertibleObj = irtn as IConvertible;
                    if (typeof(T) == typeof(string))
                    {
                        return (T)(object)convertibleObj.ToString();
                    }
                    else
                    {
                        return (T)_v[columnName]; ;
                    }
                }
                else
                {
                    return (T)_v[columnName];
                }
            }
            else
            {
                return default(T);
            }
        }

        #region IModule Members


        public List<string> ColumnNames
        {
            get
            {
                return _v.Keys.ToList<string>();
            }
        }

        #endregion

        #region IModule Members


        public object GetValue(string columnName)
        {
            if (_v.ContainsKey(columnName))
            {
                return _v[columnName];
            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}
